// pages/update/update.js
const app = getApp();
Page({
  data: {
    ac_name: '',
    ac_start_time: '',
    ac_end_time: '',
    ac_num: '',
    goodsName: '',
    activity: '',
    activity_attr: '',
    id: '',
    showSku: false,
    selectedGoodsList: [], //选择的商品
    checkAllGoods: false,  //保存选择SKU是否全选
  },
  backManage() {
    wx.navigateBack({
      delta: 1
    })
    // wx.redirectTo({
    //   url: '../manage/manage',
    // })
  },
  modify_name: function (e) {
    if (e.detail.value.length > 45) {
      app.showToast('活动名称不能超过45个字')
    } else {
      this.setData({
        ac_name: e.detail.value
      })
    }
  },
  bindDateChange1: function (e) {
    this.setData({
      ac_start_time: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    this.setData({
      ac_end_time: e.detail.value
    })
  },
  modify_num: function (e) {
    this.setData({
      ac_num: e.detail.value
    })
  },
  original_price(e) {
    this.modify(e)
  },
  one_price(e) {
    this.modify(e)
  },
  discount_price(e) {
    this.modify(e)
  },
  inventory(e) {
    this.modify(e)
  },
  order_count(e) {
    this.modify(e)
  },
  modify(e) {
    var that = this;
    var ListName = e.target.dataset.name;
    var List = that.data.activity_attr;
    var modify_attr = e.detail.value;
    var ListAttr = e.detail;
    var attr_id = e.target.dataset.sku;
    List.tbody.forEach(item => {
      if (item.id == attr_id) {
        item[ListName] = modify_attr
      }
    })
    that.setData({
      activity_attr: List
    })
  },
  save_modify() {
    var that = this;
    let activity_attr = that.data.activity_attr;
    let submitArr = [];
    wx.showLoading({
      title: '保存中...',
    })
    activity_attr.tbody.forEach(i => {
      submitArr.push({
        "id": i.id,
        "original_price": i.original_price ,
        "collage_price": i.collage_price || i.discount_price,
        "inventory": i.inventory,
        "goods_attr_id": i.goods_attre_id || i.attr_id,
        "activity_attr_id": i.attribute_value_id,
        attr_img: i.attr_img || i.images
      })
    })
    if (that.data.ac_name.length > 45) {
      app.showToast('活动名称不能超过45个字')
    } else {
      wx.request({
        url: app.buildUrl('/shop/active/updataindex'),
        header: app.buildHeader(),
        method: 'POST',
        data: {
          active_attr: JSON.stringify(submitArr),
          begin_time: that.data.ac_start_time,
          end_time: that.data.ac_end_time,
          active_num: that.data.ac_num,
          active_name: that.data.ac_name,
          id: that.data.id,
          goods_id: that.data.activity.goods_id,
          status: that.data.activity.status
        },
        success: function (res) {
          wx.hideLoading();
          if (res.data.code == 200) {
            app.showToast(res.data.msg)
            setTimeout(() => {
              // wx.redirectTo({
              //   url: '../manage/manage',
              // })
              wx.navigateBack({
                delta: 1
              })
            }, 1000)
          } else {
            app.showToast(res.data.msg)
          }
        }
      })
    }
  },
  // 重新选择SKU
  getSku(){
    let that = this;
    that.setData({
      showSku:true
    })
    wx.showLoading({
      title: '加载中...',
    })
    wx.request({
      url: app.buildUrl('/shop/active/getsku'),
      method: "POST",
      header: app.buildHeader(),
      data: {
        id: that.data.activity.goods_id,
        aid: that.data.activity.id
      },
      success(res) {
        wx.hideLoading();
        let list = res.data.data.goods_attr;
        if (res.data.code == 200) {
          list.tbody.forEach(i => {
            i.check = false
          })
          that.setData({
            selectedGoodsList: list
          })
        } else {
          app.showToast(res.data.msg)
        }
      }
    })
  },
  //选择全部
  checkAll(e) {
    let that = this;
    let list = that.data.selectedGoodsList;
    let status = that.data.checkAllGoods;
    that.setData({
      checkAllGoods: !that.data.checkAllGoods
    })
    if (status) {
      list.tbody.forEach(i => {
        i.check = false;
      })
    } else {
      list.tbody.forEach(i => {
        i.check = true;
      })
    }
    that.setData({
      selectedGoodsList: list
    })
  },
  // 选择SKU
  checkGoods(e) {
    let that = this;
    let checkedId = e.target.dataset.checkId;
    let newList = that.data.selectedGoodsList;
    let num = 0;
    newList.tbody.forEach(i => {
      if (checkedId == i.id) {
        i.check = !i.check
      }
      if (i.check) {
        num++
      }
    })
    if (num == newList.tbody.length) {
      that.setData({
        checkAllGoods: true
      })
    } else if (num < newList.tbody.length) {
      that.setData({
        checkAllGoods: false
      })
    }
    that.setData({
      selectedGoodsList: newList
    })
  },
  // 确定SKU
  confrimSku(){
    let that = this;
    let checkStatus = false;
    that.data.selectedGoodsList.tbody.forEach(i => {
      if (i.check) {
        checkStatus = true
      }
    })
    if (checkStatus) {
      that.setData({
        showSku:false,
        checkAllGoods:false,
        'activity_attr.tbody': that.data.selectedGoodsList.tbody.filter(item => item.check == true)
      })
    } else {
      app.showToast("请选择SKU");
    }
  },
  // 取消SKU
  closeSku(){
    this.setData({
      checkAllGoods: false,
      showSku: false
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      id: options.id || 23
    })
    wx.request({
      url: app.buildUrl('/shop/active/select'),
      method: 'POST',
      header: app.buildHeader(),
      data: {
        id: that.data.id
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            activity: res.data.data.activity,
            activity_attr: res.data.data.activity_attr,
            ac_start_time: that.getDate(res.data.data.activity.begin_time),
            ac_end_time: that.getDate(res.data.data.activity.end_time),
            ac_num: res.data.data.activity.active_num,
            ac_name: res.data.data.activity.active_name
          })
        }
      }
    })
  },
  getDate(date) {
    let Y = new Date(date * 1000).getFullYear();
    let M = new Date(date * 1000).getMonth() + 1;
    let D = new Date(date * 1000).getDate();
    M = M < 10 ? "0" + M : M;
    D = D < 10 ? "0" + D : D;
    return `${Y}-${M}-${D}`
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})